// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {

  var that = this;
    
  wx.chooseMessageFile({

    // 选择文件成功后就准备上传文件到云存储
    success: res => {
      let name = res.tempFiles[0].name
      let fileOBJ = res.tempFiles[0];
      let relativePath = "/user/root/"
      wx.cloud.uploadFile({
        cloudPath: relativePath+`${name}`,
        filePath: res.tempFiles[0].path
      })
      .then(res => {
        // 上传文件到云存储成功后就上传文件数据到数据库
          db.collection("userlist").add({
            data: {
              fileOBJ: fileOBJ,
              relativePath: relativePath,
              name: name
            }
          })
      })
    }
  })  
}